package mhys.Dao;
import mhys.Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
    封装增删改查方法
 */
public class BaseDao {
    Connection conn =null;
    PreparedStatement stmt = null;
    ResultSet res = null;

    //增删改
    public int insertUpdateDelete(String sql,Object...objects){
        int num = 0;
        try {
            conn = DBUtil.getConn();
            stmt = conn.prepareStatement(sql);
            if (objects != null){
                for (int i = 0; i < objects.length; i++){
                    stmt.setObject(i+1,objects[i]);
                }
            }
            num = stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,stmt,conn);
        }
        return num;
    }

    //查询
    public ResultSet select(String sql,Object...objects){
        try {
            conn = DBUtil.getConn();
            stmt = conn.prepareStatement(sql);
            if (objects != null){
                for (int i = 0;i<objects.length;i++){
                    stmt.setObject(i+1,"%"+objects[i]+"%");

                }
            }
            res = stmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  res;
    }
    public ResultSet selectLogin(String sql,Object...objects){
        try {
            conn = DBUtil.getConn();
            stmt = conn.prepareStatement(sql);
            if (objects != null){
                for (int i = 0;i<objects.length;i++){
                    stmt.setObject(i+1,objects[i]);

                }
            }
            res = stmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  res;
    }
}
